//
//  ZUserAccountViewModel.m
//  OC-weibo
//
//  Created by zero on 17/3/23.
//  Copyright © 2017年 zero. All rights reserved.
//

#import "ZUserAccountViewModel.h"
#import "ZUserInfoModel.h"


#define userInfoPlist ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"userInfo.plist"])

@interface ZUserAccountViewModel()

@property (nonatomic,strong)ZUserInfoModel *model;

@end

@implementation ZUserAccountViewModel



#pragma mark
#pragma mark -单例对象

+ (instancetype)shareUserAccountViewModel{
    
    static ZUserAccountViewModel *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        

        instance = [self new];
        
    });
    

    return instance;
}

#pragma mark
#pragma mark - 重写get方法
- (ZUserInfoModel *)model{
    
    _model = [self loadUserAccount];
    
    return _model;
}

- (BOOL)isUserIn{
    
    return self.model == nil ? NO : YES;
}

- (NSURL *)urlImage{
    
    _urlImage = [NSURL URLWithString:@"http://tva3.sinaimg.cn/crop.0.0.641.641.1024/005IBQk9jw8erfblyzskbj30ht0humyy.jpg"];
    
    return _urlImage;
}

#pragma mark
#pragma mark - 获取token
- (void)getToken:(NSString *)code finishBlock:(void(^)(BOOL res))finsishBlock{
    
    NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/access_token?client_id=%@&client_secret=%@&grant_type=authorization_code&code=%@&redirect_uri=%@",app_Key,app_Secret,code,redirect_Uri];
    
    [[ZNetworkTool shareNetworkTool] POSTWithUrlString:urlString parameters:nil success:^(NSDictionary *responseObject) {
        
        
        [self getUserInfo:responseObject userInfoBlock:finsishBlock];
        
    } failure:^(NSError *error) {
        
        if (finsishBlock) {
            
            finsishBlock(NO);
        }
    }];

}

#pragma mark
#pragma mark - 根据token获取用户信息
- (void)getUserInfo:(NSDictionary *)res userInfoBlock:(void(^)(BOOL res)) userInfoBlock{
    
    NSString *urlString = @"https://api.weibo.com/2/users/show.json";
    
    NSDictionary *parameters = @{@"access_token" : res[@"access_token"],
                                 @"uid" : res[@"uid"]
                                 };
    
    [[ZNetworkTool shareNetworkTool] GETWithUrlString:urlString parameters:parameters success:^(id responseObject) {
        
//        NSLog(@"%@",responseObject);
        
        
        NSMutableDictionary *mutable = [NSMutableDictionary dictionaryWithDictionary:responseObject];
        
        [res enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            
            mutable[key] = obj;
        }];
        
        NSLog(@"%@",mutable);
        
        _model = [[ZUserInfoModel alloc] initWithDic:mutable];
        
        //  归档
        [self saveUserAccount:self.model];
        
//        NSLog(@"%@",self.model);
        
        userInfoBlock(YES);
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        if (userInfoBlock) {
            
            userInfoBlock(NO);
        }

        
    }];
}

#pragma mark
#pragma mark - 归档
- (void)saveUserAccount:(ZUserInfoModel *)model{
    
    BOOL res = [NSKeyedArchiver archiveRootObject:model toFile:userInfoPlist];
    
    if (res) {
        
        NSLog(@"归档成功");
    }else{
        NSLog(@"接档失败");
    }
}

#pragma mark
#pragma mark - 接档
- (ZUserInfoModel *)loadUserAccount{
    
    ZUserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:userInfoPlist];
    

    NSLog(@"%@",model.avatar_hd);
    return model;
}
@end
